<?php
/**
 * 后台角色权限管理
 * @author cjli
 *
 */
class RoleAction extends AdminAction
{
	public function _initialize()
	{
		parent::_initialize();
	}
	
	//角色列表
	public function index()
	{
		$roledb = D('admin_role');
		$roleList = $roledb->order('listorder DESC, id DESC')->select();
		$this->assign('roleList', $roleList);
		$this->display('role_list');
	}
	
	// 添加角色
	public function add()
	{
		if(isset($_POST['dosubmit'])){
			if(!is_array($_POST['info']) || empty($_POST['info']['rolename'])){
				$this->error('角色名称不能为空！', U('role/add'));
			}
			$roledb = D('admin_role');
			$roleid = $roledb->where(array('rolename'=>$_POST['info']['rolename']))->getField('id');
			if($roleid){
				$this->error('角色名称 '.$_POST['info']['rolename'].' 已经存在！', U('role/add'));
			}
			
			$roledb->data($_POST['info']);
			$roledb->add();
			
			$this->success('操作成功！', U('role/index'));
		}else{
			$this->display('role_add');
		}
	}
	
	//角色编辑
	public function edit()
	{
		$roledb = D('admin_role');
		if(isset($_POST['dosubmit'])){
			$roleid = intval($_POST['roleid']);
			if(!is_array($_POST['info']) || empty($_POST['info']['rolename'])){
				$this->error('角色名称不能为空！', U('role/edit', 'roleid='.$roleid));
			}
			$where['rolename'] = $_POST['info']['rolename'];
			$where['id'] = array('neq', $roleid);
			$rid = $roledb->where($where)->getField('id');
			if( $rid ){
				$this->error('角色名称 '.$_POST['info']['rolename'].' 已经存在！', U('role/edit', 'roleid='.$roleid));
			}
			
			$roledb->where('id='.$roleid)->save($_POST['info']);
			
			$this->success('操作成功！', U('role/index'));
		}else{
			$roleid = intval($_GET['roleid']);
			$role_info = $roledb->where('id='.$roleid)->find();
			$this->assign('role', $role_info);
			$this->display('role_edit');
		}
	}
	
	//删除角色
	public function delete()
	{
		$roleid = intval($_GET['roleid']);
		$roledb = D('admin_role');
		$roledb->delete($roleid);
		$this->success('删除角色成功！', U('role/index'));
	}
	
	//角色排序
	public function listorder()
	{
		if(isset($_POST['dosubmit'])) {
			$roledb = D('admin_role');
			foreach($_POST['listorders'] as $id => $listorder) {
				$roledb->where(array('id'=>$id))->save(array('listorder'=>$listorder));
			}
			$this->success('排序操作成功！');
		} else {
			$this->error('排序操作失败！');
		}
	}
	
	/**
	 * 修改角色状态
	 */
	public function change_status_ajax()
	{
		$roleid = intval($_GET['roleid']);
		$display = intval($_GET['display']);
		$roledb = D('admin_role');
		$roledb->where('id='.$roleid)->save(array('display' => $display));
		$this->success('角色状态更新成功！', U('role/index'));
	}
	
	/**
	 * 设置权限
	 */
	public function role_priv()
	{
		$roleid = intval($_REQUEST['roleid']);
		
		if(isset($_POST['dosubmit'])) {
			$menudb = D('menu');
			$privdb = D('admin_role_priv');
			$privdb->where('roleid='.$roleid)->delete(); //清空权限表
			if ( is_array($_POST['menuid']) && count($_POST['menuid'] ) > 0){
				$menuinfo = $menudb->field('id,c,a,data')->select();
				foreach ($menuinfo as $_v) $menu_info[$_v[id]] = $_v;
				foreach($_POST['menuid'] as $menuid){
					$info = $menu_info[$menuid];
					$info['roleid'] = $roleid;
					unset($info['id']);
					$rs = $privdb->where($info)->getField('roleid');
					if(is_null($rs)){
						$privdb->add($info);
					}
				}
			}
			$this->success('权限设置成功！', U('role/index'), 'edit');
		} else {
			
			import('COM.Tree');
			$tree = new Tree();
			$tree->icon = array('│ ','├─ ','└─ ');
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			$menudb = D('menu');
			$result = $menudb->order('listorder DESC, id ASC')->select();
			$privdb = D('admin_role_priv');
			$priv_data = $privdb->where('roleid='.$roleid)->select(); //获取权限表数据
			foreach ($result as $n=>$t) {
				$result[$n]['checked'] = ($this->is_checked_priv($t, $_GET['roleid'], $priv_data))? ' checked' : '';
				$result[$n]['level'] = $this->get_level($t['id'],$result);
				$result[$n]['parentid_node'] = ($t['parentid'])? ' class="child-of-node-'.$t['parentid'].'"' : '';
			}
			$str  = "<tr id='node-\$id' \$parentid_node>
						<td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuid[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$name</td>
					</tr>";
		
			$tree->init($result);
			$categorys = $tree->get_tree(0, $str);
			$this->assign('categorys', $categorys);
			$this->assign('roleid', $roleid);
			$this->display('role_priv');
		}
	}
	
	/**
	 * 某一角色下的成员管理
	 */
	public function member_manage() {
		$roleid = intval($_GET['roleid']);
		$admindb = D('admin');
		$admindb->field('admin.*,admin_role.rolename');
		$admindb->join('admin_role ON admin_role.id = admin.roleid');
		$admindb->where(array('admin.roleid'=>$roleid));
		$adminList = $admindb->select();
		$this->assign('adminList', $adminList);
		$this->display(TMPL_PATH.'/manage/admin_list.phtml');
	}

	/**
	 *  检查指定菜单是否有权限
	 * @param array $data menu表中数组
	 * @param int $roleid 需要检查的角色ID
	 * @param array $priv_data 权限列表
	 */
	private function is_checked_priv($data,$roleid,$priv_data) {
		$priv_arr = array('c','a','data');
		if($data['c'] == '') return false;
		foreach($data as $key=>$value){
			if(!in_array($key,$priv_arr)) unset($data[$key]);
		}
		$data['roleid'] = $roleid;
		$info = in_array($data, $priv_data);
		if($info){
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 获取菜单深度
	 * @param $id
	 * @param $array
	 * @param $i
	 */
	private function get_level($id,$array=array(),$i=0) {
		foreach($array as $n=>$value){
			if($value['id'] == $id)
			{
				if($value['parentid']== '0') return $i;
				$i++;
				return $this->get_level($value['parentid'],$array,$i);
			}
		}
	}
}